package cn.com.dycc.common.push_key.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.dycc.common.push_key.dao.UserChannelDao;
import cn.com.dycc.common.push_key.entity.UserChannel;
import cn.com.dycc.util.DateUtil;

@Service("userChannelService")
@Transactional
public class UserChannelServiceImpl implements UserChannelService{
	
	@Resource(name="userChannelDao")
	private UserChannelDao userChannelDao;
	
	//用户登录时，将用户id和channelid绑定
	public String insertChannel(UserChannel userChannel) throws UserChannelNotFoundException {
		
		String user_id = userChannel.getUser_id();
		if(user_id==null){
			throw new UserChannelNotFoundException("请登录！");
		}
		
		String format = "yyyy-MM-dd HH:mm:ss";
		String bind_time = DateUtil.getNowDate(format);
		userChannel.setBind_time(bind_time);
		
		String channel_id = userChannel.getChannel_id();
		
		if(channel_id==null){
			return "2";
		}else{
			String channel = userChannelDao.findChannelIdByUserId(user_id);
			if(channel==null){
				userChannelDao.insertChannel(userChannel);
			}else{
				userChannelDao.updateChannelByUserId(userChannel);
			}
		}
		
		return "绑定成功";
	}

}
